package com.todo.partner.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.partner.infrastructure.entity.condition.ContentPartnerCondition;
import com.todo.partner.infrastructure.entity.po.ContentPartnerPO;
import com.todo.partner.infrastructure.mapper.ContentPartnerMapper;
import com.todo.partner.infrastructure.repository.ContentPartnerRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * (ContentPartner)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("contentPartnerRepository")
public class ContentPartnerRepositoryImpl implements ContentPartnerRepository {
    @Resource
    private ContentPartnerMapper contentPartnerMapper;


    @Override
    public PageResult<ContentPartnerPO> queryContentPartnerByCondition(ContentPartnerCondition contentPartnerCondition) {
        PageResult<ContentPartnerPO> pageResult = new PageResult<>();
        if (contentPartnerCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(contentPartnerCondition.getPageIndex());
        pageResult.setPageSize(contentPartnerCondition.getPageSize());
        long count = queryCountByCondition(contentPartnerCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, contentPartnerCondition.getPageSize()));
        pageResult.setTotal(count);
        List<ContentPartnerPO> sightPoiPOList = contentPartnerMapper.queryContentPartnerByCondition(contentPartnerCondition);
        if (CollectionUtils.isEmpty(sightPoiPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(sightPoiPOList);
        pageResult.setCurrentIndex(sightPoiPOList.get(0).getId());
        pageResult.setHasNext(sightPoiPOList.size() >= contentPartnerCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(ContentPartnerCondition contentPartnerCondition) {
        if (contentPartnerCondition == null || contentPartnerCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return contentPartnerMapper.queryCountByCondition(contentPartnerCondition);
    }

    @Override
    public Boolean insertContentPartner(ContentPartnerPO contentPartnerPO) {
        if (contentPartnerPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(contentPartnerPO)) {
            return false;
        }
        int partnerSave = contentPartnerMapper.insertContentPartnerPO(contentPartnerPO);
        if (partnerSave == 0) {
            log.warn("ContentPartnerRepository#insertContentPartner 新增搭子内容完成，新增失败，默认返回失败 contentPartnerPO：【{}】", JSONObject.toJSONString(contentPartnerPO));
            throw new BizException(BizErrorCodeEnum.SYSTEM_ERROR);
        }
        return true;
    }

    @Override
    public Boolean updateContentPartnerById(ContentPartnerPO contentPartnerPO) {
        if (contentPartnerPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(contentPartnerPO)) {
            return false;
        }
        ContentPartnerPO alreadyContentPartnerPO = queryPartnerContentDOById(contentPartnerPO.getId());
        if (alreadyContentPartnerPO == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        if (!alreadyContentPartnerPO.getContentId().equals(contentPartnerPO.getContentId())) {
            throw new BizException(BizErrorCodeEnum.ILLEGAL_OPERATION, "禁止替换新的内容id");
        }

        int partnerSave = contentPartnerMapper.updateContentPartnerPOById(contentPartnerPO);
        if (partnerSave == 0) {
            log.warn("ContentPartnerRepository#insertContentPartner 更新搭子内容完成，新增失败，默认返回失败 contentPartnerPO：【{}】", JSONObject.toJSONString(contentPartnerPO));
            throw new BizException(BizErrorCodeEnum.SYSTEM_ERROR);
        }
        return true;
    }

    @Override
    public Boolean removeContentPartnerById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        ContentPartnerPO contentPartnerPO = queryContentPartnerPOById(id);
        if (contentPartnerPO == null) {
            log.warn("ContentPartnerRepository#removeContentPartnerById 查询搭子关联内容为空，默认返回成功 id：【{}】", id);
            return true;
        }
        int removeCount = contentPartnerMapper.removeContentPartnerPOById(id);
        if (removeCount == 0) {
            log.warn("ContentPartnerRepository#removeContentPartnerById 根据主键删除内容搭子关联失败，默认返回失败 id：【{}】", id);
            return false;
        }
        return true;
    }

    @Override
    public ContentPartnerPO queryContentPartnerPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        ContentPartnerPO contentPartnerPO = contentPartnerMapper.queryContentPartnerPOById(id);
        return contentPartnerPO;
    }

    @Override
    public ContentPartnerPO queryPartnerContentDOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        ContentPartnerPO contentPartnerPO = queryContentPartnerPOById(id);
        if (contentPartnerPO == null) {
            log.warn("ContentPartnerRepository#queryPartnerContentDOById 查询搭子关联内容为空，默认返回空数据 id：【{}】", id);
            return null;
        }
        return contentPartnerPO;
    }

    @Override
    public ContentPartnerPO queryContentPartnerPOByContentId(Long contentId) {
        if (contentId == null || contentId < 0) {
            return null;
        }
        return contentPartnerMapper.queryContentPartnerPOByContentId(contentId);
    }

    @Override
    public Boolean removeContentPartnerByContentId(Long contentId) {
        if (contentId == null || contentId < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        ContentPartnerPO contentPartnerPO = queryContentPartnerPOByContentId(contentId);
        if (contentPartnerPO == null) {
            log.info("ContentPartnerRepository#removeContentPartnerByContentId 根据内容id查询数据为空，默认返回true contentId：【{}】", contentId);
            return true;
        }
        return removeContentPartnerById(contentPartnerPO.getId());
    }

    @Override
    public List<ContentPartnerPO> queryContentPartnerList(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Collections.emptyList();
        }

        return contentPartnerMapper.queryContentPartnerList(idList);
    }

    private boolean baseFieldCheck(ContentPartnerPO contentPartnerPO) {
        if (contentPartnerPO == null) {
            return false;
        }
        if (contentPartnerPO.getContentId() == null || contentPartnerPO.getUserId() == null || contentPartnerPO.getPartnerContentType() == null) {
            log.warn("ContentPartnerRepository#baseFieldCheck 内容搭子核心字段异常，操作失败，默认返回失败 contentPartnerPO：【{}】", JSONObject.toJSONString(contentPartnerPO));
            return false;
        }
        if (contentPartnerPO.getProvince() == null || contentPartnerPO.getCity() == null || contentPartnerPO.getStartPlanTime() == null || contentPartnerPO.getEndPlanTime() == null) {
            log.warn("ContentPartnerRepository#baseFieldCheck 内容搭子关联字段异常，操作失败，默认返回失败 contentPartnerPO：【{}】", JSONObject.toJSONString(contentPartnerPO));
            return false;
        }
        return true;
    }
}
